#include "widget.h"
#include "ui_widget.h"
#include <QtGui/QPainter>
#include <QDebug>
#include <QIcon>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->widget->installEventFilter(this);
    this->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏
    ui->Btn_saomiao->setFlat(true);
    ui->Btn_key->setFlat(true);
    ui->Btn_pic->setFlat(true);
    ui->frame_menu->setWindowFlags(Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
    /*ui->frame->setWindowFlags(Qt::FramelessWindowHint);
    ui->frame->setWindowOpacity(0);
    ui->frame->setAttribute(Qt::WA_TranslucentBackground);*/
    //*******************button**************
    btn[0] = ui->btn0;
    btn[1] = ui->btn1;
    btn[2] = ui->btn2;
    btn[3] = ui->btn3;
    btn[4] = ui->btn4;
    btn[5] = ui->btn5;
    btn[6] = ui->btn6;
    btn[7] = ui->btn7;
    btn[8] = ui->btn8;
    btn[9] = ui->btn9;
    btn[10] = ui->btn0_1;
    btn[11] = ui->btn0_2;
    for(int i = 0;i<12;i++)
    {
        connect(btn[i],SIGNAL(clicked()),this,SLOT(btnclick()));
    }
    //*******************timer**************
    timer = new QTimer(this);
    timer->setInterval(1);
    //connect(timer,SIGNAL(timeout()),this,SLOT(update()));
    connect(timer, &QTimer::timeout, this, [this](){ update(); });
    timer->start();

    timer_pic = new QTimer(this);
    timer_pic->setInterval(1000);
    connect(timer_pic,SIGNAL(timeout()),this,SLOT(on_timer()));
    //**************************************
    ui->ImageBtn->hide();
    ui->Btn_close->hide();
    ui->frame_key->hide();
    ui->frame_menu->setWindowFlags(Qt::WindowStaysOnTopHint);
    ui->frame_menu->show();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::btnclick()
{
    QPushButton *optBtn = qobject_cast<QPushButton *>(sender());

    QString name = sender()->objectName();
    for(int i=0;i<12;i++)
    {
        if(btn[i] == optBtn)
        {
            //qDebug()<<"click:"<<i;
            //ui->lineEdit->setText(ui->lineEdit->text()+QString::number(i));
            ui->lineEdit->setText(ui->lineEdit->text()+btn[i]->text());
        }
    }
    qDebug()<<"name = "<<name;
}
/*void Widget::changeEvent(QEvent *e) {

    QWidget::changeEvent(e);

    switch (e->type()) {

    case QEvent::LanguageChange:

        ui->retranslateUi(this);
        break;

    default:
        break;
    }
}*/
//鼠标移动事件
void Widget::mouseMoveEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        qDebug() << "左键移动" ;
    }
    else if(event->button() == Qt::RightButton)
    {
        qDebug() << "右键移动" ;
    }

    //获取移动过程中的鼠标指针坐标
    //qDebug() << "move:" << event->x() << " " << event->y();
    if (old_y == 0)
        old_y = event->y();
    else
    {
        slide_y = event->y() - old_y;
        old_y = event->y();
    }

    if (slide_y > 0)
    {
        /*frame_y = frame_y + slide_y;
        if (frame_y < 0)
            ui->frame_menu->move(0,frame_y);
        else*/
            ui->frame_menu->move(0,0);
    }
    else
    {
        /*frame_y = frame_y + slide_y;
        if (frame_y > -110)
            ui->frame_menu->move(0,frame_y);
        else*/
            ui->frame_menu->move(0,-110);
    }


}
bool Widget::eventFilter(QObject *watched, QEvent *e) {

    if (watched == ui->widget) {
        if (e->type() == QEvent::Paint) {
            paintOnWidget(ui->widget);
            return true;
        }
    }
    return QWidget::eventFilter(watched, e);
}


void Widget::paintOnWidget(QWidget *w) {

    QPainter painter(w);
    painter.setRenderHint(QPainter::Antialiasing,true);
    //int min = int(qMin(width(), height()));
    int radius = 780;
    /*painter.setPen(QPen(qRgba(120, 110, 250, 250)));
    //格子
    for(int i = 0; i < width(); i += 20)
        painter.drawLine(i + 0.5, 0, i + 0.5, height());
    for(int j = 0; j < height(); j += 20)
        painter.drawLine(0, j + 0.5, width(), j + 0.5);


    QPoint center(int(radius / 2), int(448));
    painter.drawEllipse(center, radius / 2, radius / 2);
    painter.drawEllipse(center, radius / 3, radius / 3);
    painter.drawEllipse(center, radius / 6, radius / 6);*/
    if (1)
    {
        int diff = int(qAbs(radius/2- 448));
        QConicalGradient gradient(radius/2, 448, m_angle+180);
        gradient.setColorAt(0.2, QColor(108, 156, 229,255));
        gradient.setColorAt(0.7, QColor(108, 156, 229,0));
        painter.setBrush(gradient);
        painter.setPen(QPen(Qt::NoPen));

        painter.drawPie(0, diff, radius, radius, m_angle * 16, 60 * 16);
        /*if (width() > height())
            painter.drawPie(diff, 0, radius, radius, m_angle * 16, 60 * 16);
        else painter.drawPie(0, diff, radius, radius, m_angle * 16, 60 * 16);

        for(int i = 0; i < 5; ++i)
        {
            painter.setBrush(QBrush(QColor(184, 218, 167, 0)));
            //painter.drawEllipse(m_points.at(i).point, 7, 7);
        }*/
        if (m_angle <= -360)
            m_angle = 0;
        else
            m_angle -= 2;
       // qDebug("m_angle is : %d", m_angle);
    }

    /*painter.setRenderHint(QPainter::Antialiasing, true);//反走样功能，调用反锯齿功能

    int radius = 300;
    int arcHeight = 10;

    // >> 1（右移1位）相当于width() / 2
    painter.translate(width() >> 1, height() >> 1);*/

    /**
     * 参数二：半径
     * 参数三：开始的角度
     * 参数四：指扫取的角度-顺时针（360度 / 8 = 45度）
     * 参数五：圆环的高度
     * 参数六：填充色
    **/
    /*gradientArc(&painter, radius,rotate,  45, arcHeight, qRgba(184, 218, 167,0));
    if (rotate < 360)
        rotate = rotate + 1;
    else
        rotate = 0;*/

    //***********************************
        /*QPainter painter(this);
            QPixmap disc(":/disc.png");

            //碟机转动
            if(imageRotate++ == 360)
                imageRotate = 0;
            // 设定旋转中心点
            painter.translate(130,150);
            // 旋转的角度
            painter.rotate(imageRotate);
            // 恢复中心点
            painter.translate(-130,-150);
            // 画图操作
            painter.drawPixmap(40,60,180,180, disc);  */
    //************************************
}
void Widget::gradientArc(QPainter *painter, int radius, int startAngle, int angleLength, int arcHeight, QRgb color)
{
    // 渐变色
    QRadialGradient gradient(0, 0, radius);
    gradient.setColorAt(0.1, QColor(255, 0, 127, 150));
    gradient.setColorAt(0.7, color);
    painter->setBrush(gradient);

    // << 1（左移1位）相当于radius*2 即：150*2=300
    //QRectF(-150, -150, 300, 300)
    QRectF rect(-radius, -radius, radius << 1, radius << 1);
    QPainterPath path;
    path.arcTo(rect, startAngle, angleLength);

    painter->setPen(Qt::NoPen);
    painter->drawPath(path);
}

void Widget::on_timer()
{
   ///////////////////////////////////////////////////////////////////
   if (index>=4)
       index = 0;
   else
       index++;

   QIcon ico(images[index]);

   ui->ImageBtn->setIcon(ico);
   ui->ImageBtn->setIconSize(QSize(800,1280));
   ui->ImageBtn->setFlat(false);
   /////////////////////////////////////////////////////////////////////
}

void Widget::on_Btn_close_clicked()
{
    timer_pic->stop();
    ui->ImageBtn->hide();
    ui->Btn_close->hide();
    ui->widget->show();
    ui->frame_menu->setWindowFlags(Qt::WindowStaysOnTopHint);
    ui->frame_menu->show();
    ui->frame_menu->move(0,-110);
}

void Widget::on_Btn_key_clicked()
{
    ui->frame_key->show();
    ui->widget->hide();
    ui->ImageBtn->hide();
    ui->frame_menu->setWindowFlags(Qt::WindowStaysOnTopHint);
    ui->frame_menu->show();
    ui->frame_menu->move(0,-110);
}

void Widget::on_Btn_pic_clicked()
{
    timer_pic->start();
    ui->frame_key->hide();
    ui->widget->hide();
    ui->ImageBtn->show();
    ui->Btn_close->show();
    ui->frame_menu->hide();
}

void Widget::on_Btn_saomiao_clicked()
{
    ui->widget->show();
    ui->frame_key->hide();
    ui->frame_menu->setWindowFlags(Qt::WindowStaysOnTopHint);
    ui->frame_menu->show();
    ui->frame_menu->move(0,-110);
}

void Widget::on_btn_del_clicked()
{
    string str = (ui->lineEdit->text()).toStdString();
    if (str != "")
    {
       str = str.substr(0, str.length() - 1);
       ui->lineEdit->setText(QString::fromStdString(str));
    }
}

void Widget::on_btn_clear_clicked()
{
    string str = "";
    ui->lineEdit->setText(QString::fromStdString(str));
}

void Widget::on_btn_ok_clicked()
{
    string str = "";
    ui->lineEdit->setText(QString::fromStdString(str));
}
